/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.battletank;

import com.nokia.example.battletank.menu.BuyMenu;
import com.nokia.mid.payment.IAPClientPaymentException;
import com.nokia.mid.payment.IAPClientPaymentListener;
import com.nokia.mid.payment.IAPClientPaymentManager;
import com.nokia.mid.payment.IAPClientProductData;
import com.nokia.mid.payment.IAPClientUserAndDeviceData;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

public class Main extends MIDlet implements IAPClientPaymentListener {
    public static final String PURCHASE_ID = "681803";
    private static boolean trial = true;
    private static IAPClientPaymentManager manager;
    private static Display display;
    private BattleTankCanvas battleTankCanvas = null;    

    public void startApp() {
        if(battleTankCanvas == null) {
            battleTankCanvas = new BattleTankCanvas(this);
            display = Display.getDisplay(this);
            display.setCurrent(battleTankCanvas);
        }
        manager = getIAPManager();
        manager.setIAPClientPaymentListener(this);
        
        // If the trial flag has not been set to false in record store,
        // query the status through In-app Puchase API. The boolean gets
        // set during the instantiation of battleTankaCanvas
        if(trial) {
            manager.getProductData(PURCHASE_ID);
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        if(battleTankCanvas != null) battleTankCanvas.saveGame();
    }

    public void close() {
        destroyApp(true);
        notifyDestroyed();
    }
    
    public static boolean isTrial() {
        return trial;
    }
    
    public static void setTrial(boolean t) {
        trial = t;
    }

    public static boolean purchaseFullVersion() {
        int status = manager.purchaseProduct(PURCHASE_ID,
                IAPClientPaymentManager.FORCED_AUTOMATIC_RESTORATION);
        if(status != IAPClientPaymentManager.SUCCESS)
        {
            showAlertMessage(display, "Purchase failure", "Purchase process failed. "
                + Messages.getPaymentError(status), AlertType.ERROR);
            return false;
        }
        return true;
    }

    public static boolean restoreFullVersion() {
        int status = manager.restoreProduct(PURCHASE_ID,
                IAPClientPaymentManager.ONLY_IN_SILENT_AUTHENTICATION);
        if(status != IAPClientPaymentManager.SUCCESS)
        {
            showAlertMessage(display, "Restoration failure", "Restoration process failed. "
                + Messages.getPaymentError(status), AlertType.ERROR);
            return false;
        }
        return true;
    }

    // Functions necessary to implement IAPClientPaymentListener interface
    public void userAndDeviceDataReceived(int status, IAPClientUserAndDeviceData ud) {}
    public void restorableProductsReceived(int status, IAPClientProductData[] productDataList) {}
    public void productDataListReceived(int status, IAPClientProductData[] productDataList) {}

    public void productDataReceived(int status, IAPClientProductData pd) {
        if(status == OK) BuyMenu.setPrice(pd.getPrice());
    }

    public void purchaseCompleted(int status, String purchaseTicket) {
        battleTankCanvas.hideBuyMenuWaitIndicator();
        if(status == OK) {
            setTrial(false);
            battleTankCanvas.hideBuyOption();
            battleTankCanvas.hideBuyMenu();
        } else {
            showAlertMessage("Purchase failure", "Purchase process failed. "
                + Messages.getPaymentError(status), AlertType.ERROR);
        }
    }

    public void restorationCompleted(int status, String purchaseTicket)
    {
        battleTankCanvas.hideBuyMenuWaitIndicator();
        if(status == OK) {
            setTrial(false);
            battleTankCanvas.hideBuyOption();
            battleTankCanvas.hideBuyMenu();
        } else {
            showAlertMessage("Restoraiton failure", "Restoration failed. "
                    + Messages.getPaymentError(status), AlertType.ERROR);
        }
    }

    public static IAPClientPaymentManager getIAPManager() {
        if(manager == null) {
            try {
                manager = IAPClientPaymentManager.getIAPClientPaymentManager();
            } catch(IAPClientPaymentException ipe) {}
        }
        return manager;
    }

    public void showAlertMessage(String title, String alertText, AlertType type) {
        showAlertMessage(display, title, alertText, type);
    }

    public static void showAlertMessage(Display display, String title,
            String alertText, AlertType type) {
        Alert alert = new Alert(title, alertText, null, type);
        display.setCurrent(alert, display.getCurrent());
    }
}
